package com.example.maxboxtwo.utils.listeners;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.TextView;

import com.example.maxboxtwo.components.function.Gradienter.GradienterView;

public class HorizontalListener implements SensorEventListener {
    // define a max angle which the gradient can handle with,as long as this angle surpassed by others ,the bubble is located in the border
   private float geomagnetic[] = new float[3];   // a array contain three values (x axis , y axis ,z axis) of gravity
   private float accelerate[] = new float[3];    // a array contain three values (x axis , y axis ,z axis) of accelerate
   private TextView left,right;
   private double axisX = 0,axisY=0;
   private double MaxAngle = 30;
   private GradienterView view;

    public HorizontalListener(TextView left, TextView right,GradienterView view) {
        this.left = left;
        this.right = right;
        this.view = view;
    }

    public float[] getGeomagnetic() {
        return geomagnetic;
    }

    public void setGeomagnetic(float[] geomagnetic) {
        this.geomagnetic = geomagnetic;
    }

    public float[] getAccelerate() {
        return accelerate;
    }

    public void setAccelerate(float[] accelerate) {
        this.accelerate = accelerate;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()){
            case Sensor.TYPE_MAGNETIC_FIELD:
                setGeomagnetic(event.values);
                calculateOrientation(left,right);
                break;
            case Sensor.TYPE_ACCELEROMETER:
                setAccelerate(event.values);
                calculateOrientation(left,right);
                break;
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
    /**
     * left is representing x axis,right is representing y axis
     *
     * values[0] is z axis ,values[1]is x axis, values[2] is y axis
     */
    public void calculateOrientation(TextView left, TextView right){
        float[] R = new float[9];
        float[]values = new float[3];
        SensorManager.getRotationMatrix(R,null,getAccelerate(),getGeomagnetic());
        SensorManager.getOrientation(R,values);
        values[0] = (float) Math.toDegrees(values[0]);
        values[1] = (float) Math.toDegrees(values[1]);
        values[2] = (float) Math.toDegrees(values[2]);

        if((Math.abs(values[1]-axisX)>2)||(Math.abs(values[2]-axisY)>2)){
            axisX = values[1];  //set axisX
            axisY = values[2];  //set axisY
            left.setText(String.valueOf(values[1]));
            left.setTextSize(12);
            right.setText(String.valueOf(values[2]));
            right.setTextSize(12);

            calculateBubbleLocation(values[1],values[2]);
        }

//        Log.i("show x axis",values[1]+"");
//        Log.i("show y axis",values[2]+"");
    }

    private void calculateBubbleLocation(float yAngle,float zInclination){
            // when the bubble is located in center of background, the gradient is horizontal
            int coordinateX = (view.background.getWidth()-view.bubble.getWidth())/2;
            int coordinateY = (view.background.getHeight()-view.bubble.getHeight())/2;
            Log.i("target+++++coordinateX",coordinateX+"");
            Log.i("target+++++coordinateY",coordinateY+"");
            // if the angle is contained by max angle
            if(Math.abs(zInclination)<=MaxAngle){
            //we can calculate the variable value by z axis's inclination
                int deltax = (int)((view.background.getWidth()-view.bubble.getWidth())/2*zInclination/MaxAngle);
                coordinateX+=deltax;
            }else if(zInclination>MaxAngle){
                //if the inclination with zAxis is bigger than maxAngle,the bubble is located in the left
                coordinateX = 504;
            }else{
                // if the inclination with zAxis is lower than minimumAngle,the bubble is located in right
                coordinateX = 121;
            }
            // if the inclination with y is contained maxAngle
            if(Math.abs(yAngle)<=MaxAngle){
                int deltaY = (int)((view.background.getHeight()-view.bubble.getHeight())/2*yAngle/MaxAngle);
                coordinateY-=deltaY;
            }else if(yAngle>MaxAngle){
                coordinateY = 692;
            }else {
                coordinateY= 151;
            }
            // calculating the coordinate of x and y in the table , update the coordinate of bubble
        Log.i("flag",isContain(coordinateX,coordinateY)+"");
                Log.i("coordinateX----------",coordinateX+"");
                Log.i("coordinateY",coordinateY+"");
                if(isContain(coordinateX,coordinateY)){
                    view.setxPosition(coordinateX);
                    view.setyPosition(coordinateY);
                    Log.i("coordinateY",coordinateY+"");
                    Log.i("coordinateX+++++++++++",coordinateX+"");
                    view.postInvalidate();
                }
    }

    //there is a bug
    public boolean isContain(int x,int y){
        if((x>121&&x<504)&&(y>152&&y<692)){
            return true;
        }else{
            return false;
        }
    }

}

